package com.gitee.cui.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Test;

/**
 * 入站处理类测试程序
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/21 16:58
 **/
public class InboundHandlerTest {
    @Test
    public void inboundHandlerTest() {
        InboundHandler inboundHandler = new InboundHandler();
        ChannelInitializer<EmbeddedChannel> channelInitializer = new ChannelInitializer<EmbeddedChannel>() {
            @Override
            protected void initChannel(EmbeddedChannel ch) throws Exception {
                ch.pipeline().addLast(inboundHandler);
            }
        };
        // 创建嵌入式通道
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(channelInitializer);
        ByteBuf buf = Unpooled.buffer();
        buf.writeInt(1);
        // 模拟入站，向嵌入式通道写一个入站数据包
        embeddedChannel.writeInbound(buf);
        embeddedChannel.flush();
        // 模拟入站，再协议额入站数据包
        embeddedChannel.writeInbound(buf);
        embeddedChannel.flush();
        // 关闭通道
        embeddedChannel.close();
    }
}
